package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.config.R;
import com.example.dao.LoginDao;
import com.example.entity.Employee;
import com.example.service.LoginService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class LoginServiceImpl extends ServiceImpl<LoginDao,Employee> implements LoginService {

    @Resource
    private LoginDao loginDao;

    @Override
    public R<Employee> login(Employee employee) {
        //检查用户输入的用户名是否存在
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee selectone= loginDao.selectOne(queryWrapper);
        if (selectone==null){
            return R.error("用户不存在");
        }

        String hex = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //判定密码是否存在
        if (hex.equals(selectone.getPassword())){
            return R.error("密码不正确");

        }
        //判定状态
        if (selectone.getStatus()==1){
            return R.error("账号已被禁用");
        }
        return R.success(selectone);
    }



    @Override
    public void add(Employee employee) {
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()).toString());
        loginDao.insert(employee);
    }

    @Override
    public void updateEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Employee> queryWrapper = new LambdaUpdateWrapper<>();
        //判断用户名
        if (employee.getName()!=null&&employee.equals("")){
            queryWrapper.set(Employee::getName,employee.getName());

        }
        //判断登录名
        if (employee.getUsername()!=null&&employee.equals("")){
            queryWrapper.set(Employee::getUsername,employee.getUsername());

        }

        //判断手机号
        if (employee.getPhone()!=null&&employee.equals("")){
            queryWrapper.set(Employee::getPhone,employee.getPhone());

        }
        //判断性别
        if (employee.getSex()!=null&&employee.equals("")){
            queryWrapper.set(Employee::getSex,employee.getSex());

        }
        //判断身份证号
        if (employee.getIdNumber()!=null&&employee.equals("")){
            queryWrapper.set(Employee::getIdNumber,employee.getIdNumber());

        }
        //判断状态
        if (employee.getStatus()!='0'&&employee.equals("")){
            queryWrapper.set(Employee::getStatus,employee.getStatus());

        }
        queryWrapper.eq(Employee::getId,employee.getId());
        loginDao.update(employee,queryWrapper);
    }

    @Override
    public R findById(Long id) {
        return R.success(loginDao.selectById(id));
    }
//    @Override
//    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        log.info("扩展消息转换器...");
//        //创建消息转换器对象
//        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
//        //设置对象转换器，底层使用Jackson将Java对象转为json
//        messageConverter.setObjectMapper(new JacksonObjectMapper());
//        //将上面的消息转换器对象追加到mvc框架的转换器集合中
//        converters.add(0,messageConverter);
//    }

}
